package com.wyh.controller;

import com.wyh.Mapper.DepartmentMapper;
import com.wyh.Mapper.UserMapper;
import com.wyh.pojo.Department;
import com.wyh.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserMapper userMapper;


    @RequestMapping("/users")
    public String list(Model model){
        List<User> all = userMapper.getUserList();

        model.addAttribute("users",all);
        return "user/list";
    }
    @GetMapping("/user")
    public String toAddPage(Model model){
        return "user/add";
    }

    @PostMapping("/user")
    public String addUser(User user){
        userMapper.addUser(user);
        return "redirect:/users";
    }

    @GetMapping("/toEditUser/{id}")
    public String toEdit(@PathVariable("id") Integer id, Model model){
        User user = userMapper.getUserById(id);
        model.addAttribute("user",user);
        return "user/update";
    }

    @PostMapping("/updateUser")
    public String updateUser(User user){
        userMapper.updateUser(user);
        return "redirect:/users";
    }

    @GetMapping("/deleteUser/{id}")
    public String delete(@PathVariable("id") Integer id){
        userMapper.deleteUser(id);
        return "redirect:/users";
    }

    @RequestMapping("/searchUser")
    public String searchUser(String name, Model model){
        User userList = userMapper.getUserByName(name);
        model.addAttribute("users",userList);
        return "user/list";
    }
}
